package cn.com.w.mybatisplus.aop;

import cn.com.w.mybatisplus.domain.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 拦截器
 */
@Component
@Aspect
@Slf4j
public class UserInfoAop {

    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    /**
     * 定义切点，切点方法不用任何代码，返回值是void，重要的是条件表达式
     */
    @Pointcut("execution(public * cn.com.w.mybatisplus.service.IUserInfoService.updateById(..))")
    public void updateById(){

        System.out.println("");
    }

    /**
     * 此种方法无需定义切点。
     *
     * @param joinPoint
     */
    @Before("execution(public * cn.com.w.mybatisplus.service.IUserInfoService.update(..))")
    public void befordUpdate(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        UserInfo userInfo = (UserInfo)args[0];
        // 本线程保存变量
        threadLocal.set(userInfo.getUserAge());
        System.out.println("ok");
    }


    @After("execution(public * cn.com.w.mybatisplus.service.IUserInfoService.update(..))")
    public void afterUpdate(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        UserInfo userInfo = (UserInfo)args[0];
        // 本线程取出变量
        Integer age = threadLocal.get();
        System.out.println("ok");
    }

    @After("updateById()")
    public void afterUpdateById(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        UserInfo userInfo = (UserInfo)args[0];
        System.out.println("ok");
    }




}





